<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/images/heima.png" alt="">
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse? '64px': '200px'">
        <div class="collapse_zoom" @click="changeCollapse">| | |</div>
        <!-- 侧边栏菜单选项 -->
        <el-menu background-color="rgb(211, 67, 204)"
          text-color="#fff"
          active-text-color="#333"
          :default-active="activity" unique-opened :collapse="isCollapse" :collapse-transition="false" router>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本区域 -->
              <span class="icon_font">{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activity: ''
    }
  },
  created () {
    this.getMenuList()
    this.activity = window.sessionStorage.activePath
  },
  methods: {
    logout: function () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      this.menuList = res.data
    },
    changeCollapse: function () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState: function (path) {
      window.sessionStorage.setItem('activePath', path)
      this.activity = path
    }
  }
}
</script>

<style lang="less" scoped>
.home-container{
  height: 100%;
}

.el-header {
  width: 100%;
  height: 200px;
  background: whitesmoke;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  color: #333;
  font-size: 20px;
  > div{
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside{
  background: #ca31c3;
  .el-menu{
    border-right: 0px;
    :hover{
      background: rgb(155, 63, 152) !important;
    }
  }
}

.collapse_zoom{
  height: 45px;
  color: #fff;
  text-align: center;
  line-height: 45px;
  font-size: 18px;
  cursor: pointer;
  background: cornflowerblue;
}

.icon_font{
  margin-left: 10px;
}

.Main{
  background: rgb(117, 107, 107);
}
</style>
